package com.beehoo.dcportal.common.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.model.LogBean;
import com.beehoo.dcportal.model.MenuBean;
import com.beehoo.dcportal.model.PortletBean;
import com.beehoo.dcportal.model.StreetBean;
import com.beehoo.dcportal.model.ThemeBean;
import com.beehoo.dcportal.service.ICommonServ;
import com.beehoo.dcportal.utils.CommonUtils;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {
	private static final long serialVersionUID = -7917062196853857424L;
	private MenuBean currMenu;
	private MenuBean currSubMenu;
	private String domain;
	private String param;
	protected Map<String, String> paramMap = new HashMap<String, String>();
	
	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}
	
	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public MenuBean getCurrSubMenu() {
		return currSubMenu;
	}

	public void setCurrSubMenu(MenuBean currSubMenu) {
		this.currSubMenu = currSubMenu;
	}

	public MenuBean getCurrMenu() {
		return currMenu;
	}

	public void setCurrMenu(MenuBean currMenu) {
		this.currMenu = currMenu;
	}

	//注入业务对象
	private ICommonServ commServ;
	public void setCommServ(ICommonServ commServ) {
		this.commServ = commServ;
	}
	
	public ICommonServ getCommServ() {
		return commServ;
	}

	//页面提示信息
	private String message;
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	private HttpServletRequest request;
	@Override
	public void setServletRequest(HttpServletRequest req) {
		request = req;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	private HttpServletResponse response;
	@Override
	public void setServletResponse(HttpServletResponse res) {
		response = res;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	private Map<String, Object> session;
	@Override
	public void setSession(Map<String, Object> mSession) {
		session = mSession;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}

	public LogBean setLog(String function, String content) {
		String userId = CommonUtils.nvlToString((String) session
				.get(Constant.SESSION_USER_ID));
		String userName = CommonUtils.nvlToString((String) session
				.get(Constant.SESSION_USER_NAME));
		String userIPIMSI = CommonUtils.nvlToString((String) session
				.get(Constant.SESSION_USER_IPIMSI));

		LogBean logBean = new LogBean();
		logBean.setUserId(userId);
		logBean.setUserName(userName);
		logBean.setUserIPIMSI(userIPIMSI);
		logBean.setLogOrigin("3");
		logBean.setFunction(function);
		logBean.setContent(content);
		return logBean;
	}

	@SuppressWarnings("unchecked")
	public String init() {
		boolean isChangeStreet = false;
		if(domain == null || domain.equals("")){
			return ERROR;
		}
		
		String sDomain  = CommonUtils.nvlToString((String) session.get(Constant.SESSION_STREET_DOMAIN));
		String streetId = CommonUtils.nvlToString((String) session.get(Constant.SESSION_STREET_ID));
		
		if(!sDomain.equals(domain) || streetId.equals("")){
			isChangeStreet = true;
			try {
				StreetBean street = commServ.queryStreet(domain);
				
				if(street == null 
						|| street.getStreetId() == null 
						|| street.getStreetId().equals("")){
					return ERROR;
				}
				
				streetId = CommonUtils.nvlToString(street.getStreetId());
				session.put(Constant.SESSION_STREET_ID, streetId);
				session.put(Constant.SESSION_STREET_DOMAIN, domain);
			} catch (BaseException e) {
				return ERROR;
			}
		}
		
		//系统主题
		ThemeBean theme = (ThemeBean)session.get(Constant.SESSION_STREET_THEME);
		if(theme == null || isChangeStreet){
			try {
				theme = commServ.queryTheme(streetId);
				session.put(Constant.SESSION_STREET_THEME, theme);
			} catch (BaseException e) {
				return ERROR;
			}
		}
		
		//系统导航
		List<MenuBean> menuLs = (List<MenuBean>)session.get(Constant.SESSION_STREET_MENU);
		if(menuLs == null || isChangeStreet) {
			try {
				menuLs = commServ.queryNavMenu(streetId);
				session.put(Constant.SESSION_STREET_MENU, menuLs);
			} catch (BaseException e) {
				return ERROR;
			}
		}
		
		//页面组件
		List<PortletBean> portletLs = (List<PortletBean>)session.get(Constant.SESSION_STREET_PORTLET);
		if(portletLs == null || isChangeStreet){
			try {
				portletLs = commServ.queryPortlets(streetId, theme.getThemeId());
				session.put(Constant.SESSION_STREET_PORTLET, portletLs);
			} catch (BaseException e) {
				return ERROR;
			}
		}
		
		//中心编号存入页面参数中
		paramMap = parseParams();
		paramMap.put("streetId", streetId);
		
		return SUCCESS;
	}
	
	//解析参数
	private Map<String, String> parseParams() {
		String[] paramName = {"msgType", "msgId", "pageNo", "rows"};
		Map<String, String> paramMap = new HashMap<String, String>();
		
		if(param == null || param.equals("")){
			return paramMap;
		}
		
		String[] params = param.split("/");
		int iNameLen   = paramName.length;
		int iValueLen  = params.length;
		
		for(int i = 0; i < iNameLen && i < iValueLen; i++){
			paramMap.put(paramName[i], params[i]);
		}
		
		if(iValueLen > iNameLen){
			for(int k = iNameLen; k < iValueLen; k++){
				paramMap.put("param["+k+"]", params[k]);
			}
		}
		
		return paramMap;
	}
	
	@SuppressWarnings("unchecked")
	private MenuBean getMenu(String menuId){
		if("".equals(menuId)){
			return new MenuBean();
		}
		
		List<MenuBean> menuLs = (List<MenuBean>)session.get(Constant.SESSION_STREET_MENU);
		
		for(MenuBean menu : menuLs){
			if(menuId.equals(menu.getMenuId())){
				return menu;
			}
		}
		
		return new MenuBean();
	}
	
	protected void setClickedMenu(String menuId){
		MenuBean menuBean = getMenu(menuId);
		setCurrSubMenu(menuBean);
		
		if(!"".equals(menuBean.getParentMenu())){
			menuBean = getMenu(menuBean.getParentMenu());
		}
		
		setCurrMenu(menuBean);
	}
	
	@SuppressWarnings("unchecked")
	protected List<MenuBean> getSubMenuList(String menuId){
		List<MenuBean> treeLs = new ArrayList<MenuBean>();
		List<MenuBean> menuLs = (List<MenuBean>)session.get(Constant.SESSION_STREET_MENU);
		
		for(MenuBean menu : menuLs){
			if(menuId.equals(menu.getParentMenu())){
				treeLs.add(menu);
			}
		}
		
		return treeLs;
	}
}